<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
	<xsl:template name="get-control-name">
		<xsl:choose>
			<xsl:when test="type='label'">lbl</xsl:when>
			<xsl:when test="type='text'">txt</xsl:when>
			<xsl:when test="type='date'">dte</xsl:when>
			<xsl:when test="type='check'">chk</xsl:when>
			<xsl:when test="type='combo'">cbe</xsl:when>
			<xsl:when test="type='lookup'">bte</xsl:when>
			<xsl:when test="type='combolookup'">cbe</xsl:when>
		</xsl:choose>
		<xsl:value-of select="name"/>
	</xsl:template>
	<xsl:template name="get-bind-type">
		<xsl:choose>
			<xsl:when test="type='text'">Bind</xsl:when>
			<xsl:when test="type='date'">Bind</xsl:when>
			<xsl:when test="type='check'">Bind</xsl:when>
			<xsl:when test="type='combo'">BindCustomType</xsl:when>
			<xsl:when test="type='lookup'">BindCustomType</xsl:when>
			<xsl:when test="type='combolookup'">BindCustomType</xsl:when>
		</xsl:choose>
	</xsl:template>
	<xsl:template name="get-bind">
		<xsl:choose>
			<xsl:when test="type='text'"><xsl:choose>
				<xsl:when test="display_format=''">BindableTextEdit(<xsl:call-template name="get-control-name"/>)</xsl:when>
				<xsl:otherwise>BindableTextEdit(<xsl:call-template name="get-control-name"/>, new DisplayConverter("<xsl:value-of select="display_format"/>"))</xsl:otherwise>
			</xsl:choose>
			</xsl:when>
			<xsl:when test="type='date'">BindableDateEdit(<xsl:call-template name="get-control-name"/>, <xsl:choose>
				<xsl:when test="allow_null='true'">true</xsl:when><xsl:otherwise>false</xsl:otherwise>
			</xsl:choose>)</xsl:when>
			<xsl:when test="type='check'">BindableCheckEdit(<xsl:call-template name="get-control-name"/>)</xsl:when>
			<xsl:when test="type='combo'">BindableComboBoxEdit(<xsl:call-template name="get-control-name"/>, <xsl:value-of select="fk_class_name"/>.GetAll(), <xsl:choose><xsl:when test="allow_null='true'">true</xsl:when><xsl:otherwise>false</xsl:otherwise></xsl:choose>)</xsl:when>
			<xsl:when test="type='lookup'">BindableButtonEdit&lt;<xsl:value-of select="fk_class_name"/>&gt;(<xsl:call-template name="get-control-name"/>, LookupHelper.TryGet<xsl:value-of select="fk_class_name"/>, <xsl:value-of select="allow_null"/>)</xsl:when>
			<xsl:when test="type='combolookup'">NotImplement</xsl:when>
		</xsl:choose>
	</xsl:template>
<xsl:template match="/root">using System;
using System.Linq;
using System.Xml.Linq;
using System.Windows.Forms;
using CSW.Framework.UI.Binding;
using CSW.Framework.UI.DevEx;
using CSW.Framework.UI.DevEx.Binding;
using CSW.Framework.UI.DevEx.UserControls;
using DevExpress.XtraEditors;
using <xsl:value-of select="name"/>.Business.Searches;
using <xsl:value-of select="name"/>.Business.Searches.Parameters;
using <xsl:value-of select="name"/>.Business.Tables;

namespace <xsl:value-of select="name"/>.Forms.Lookup
{
    internal partial class <xsl:value-of select="search_class_name"/>Form : XtraForm
    {
        private readonly TypedGridController&lt;<xsl:value-of select="search_class_name"/>&gt; m_Grid;
        private readonly Binder m_Binder;
        private readonly <xsl:value-of select="search_class_name"/>Params m_SearchParams;
        private <xsl:value-of select="table_class_name"/> m_<xsl:value-of select="table_class_name"/> = null;
        private bool m_ValidationFailedOnce = false;

        public <xsl:value-of select="search_class_name"/>Form()
        {
            InitializeComponent();

            m_SearchParams = new <xsl:value-of select="search_class_name"/>Params();

            // Bind to search parameters
            m_Binder = new Binder(m_SearchParams, new DXError(dxErrorProvider), false);
            <xsl:for-each select="control[type!='label']">m_Binder.<xsl:call-template name="get-bind-type"/>(new <xsl:call-template name="get-bind"/>, "<xsl:value-of select="property_name" />");
            </xsl:for-each>m_Binder.UpdateControls();

            // Bind to grid
            m_Grid = new TypedGridController&lt;<xsl:value-of select="search_class_name"/>&gt;(gridControl, "<xsl:value-of select="search_class_name"/>Results.csv", true);
            <xsl:for-each select="search_column[is_in_grid='true']">m_Grid.AddField("<xsl:value-of select="grid_label"/>", "<xsl:value-of select="property_name"/>"<xsl:if test="grid_format!=''">, "<xsl:value-of select="grid_format"/>"</xsl:if>);
            </xsl:for-each>m_Grid.AddRecordCount();
            m_Grid.Setup();

            m_Grid.DataSource = <xsl:value-of select="search_class_name"/>.Search(m_SearchParams);
            m_Grid.BestFitColumns();
        }
        
        public <xsl:value-of select="table_class_name"/><xsl:text> </xsl:text><xsl:value-of select="table_class_name"/>
        {
            get { return m_<xsl:value-of select="table_class_name"/>; }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (!m_Binder.Validate(m_ValidationFailedOnce))
            {
                m_ValidationFailedOnce = true;
                return;
            }

            Cursor.Current = Cursors.WaitCursor;
            m_Grid.DataSource = <xsl:value-of select="search_class_name"/>.Search(m_SearchParams);
            m_Grid.BestFitColumns();
            Cursor.Current = Cursors.Default;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            m_SearchParams.Clear();
        }

        private void gridControl_RowDoubleClick(object sender, RowDoubleClickEventArgs e)
        {
            m_<xsl:value-of select="table_class_name"/> = m_Grid.GetSelectedObject().GetBusinessObject();
            Close();
        }
    }
}
</xsl:template>
</xsl:stylesheet>